/*
 *
 *  Copyright (C) 2012-2024
 *  All rights reserved, Designed By 杭州连线客信息技术有限公司
 *  注意：
 *  本软件为杭州连线客信息技术有限公司开发研制，未经购买不得使用
 *  购买后可获得全部源代码（禁止转卖、分享、上传到码云、github等开源平台）
 *  一经发现盗用、分享等行为，将追究法律责任，后果自负
 *
 */

package com.lxk.oms.flow.supplement;

import cn.hutool.core.collection.CollUtil;
import cn.hutool.core.date.DateUnit;
import cn.hutool.core.date.DateUtil;
import cn.hutool.core.util.StrUtil;
import com.lxk.oms.order.domain.vo.OmsOrderVo;
import com.lxk.oms.order.service.IOmsOrderService;
import com.lxk.service.setting.response.PayExpireTimeDto;
import com.yomahub.liteflow.annotation.LiteflowComponent;
import com.yomahub.liteflow.core.FlowExecutor;
import com.yomahub.liteflow.core.NodeComponent;
import com.yomahub.liteflow.log.LFLog;
import com.yomahub.liteflow.log.LFLoggerManager;
import lombok.AllArgsConstructor;

import java.util.ArrayList;
import java.util.Date;
import java.util.List;
import java.util.Map;
import java.util.stream.Collectors;

/**
 * 设置支付超时
 *
 * @author huahua
 * 2024/8/29
 */
@LiteflowComponent(value = FlowPaySupplementNodeConst.SET_PAY_TIME_OUT_NODE)
@AllArgsConstructor
public class SetPayTimeOutNode extends NodeComponent {

    private final IOmsOrderService omsOrderService;
    private final LFLog log = LFLoggerManager.getLogger(FlowExecutor.class);

    @Override
    public void process() throws Exception {
        PaySupplementContext contextBean = getContextBean(PaySupplementContext.class);
        List<OmsOrderVo> wxList = contextBean.getWxPayingOrderList();
        List<OmsOrderVo> aliList = contextBean.getAliPayingOrderList();
        List<PayExpireTimeDto> payExpireTimeList = contextBean.getPayExpireTime();
        if (CollUtil.isNotEmpty(payExpireTimeList)) {
            Map<String, PayExpireTimeDto> tenantIdAndPayExpireTimeMap = payExpireTimeList.stream().collect(Collectors.toMap(PayExpireTimeDto::getTenantId, umsUserVo -> umsUserVo));
            contextBean.setWxPayingOrderList(getNewPayingList(wxList, tenantIdAndPayExpireTimeMap));
            contextBean.setAliPayingOrderList(getNewPayingList(aliList, tenantIdAndPayExpireTimeMap));
        }
    }

    /**
     * 获取支付未超时订单，把支付超时订单设置未支付超时
     *
     * @param list
     * @param map
     * @return
     */
    private List<OmsOrderVo> getNewPayingList(List<OmsOrderVo> list, Map<String, PayExpireTimeDto> map) {
        List<OmsOrderVo> payingList = new ArrayList<>();
        if (list != null && !list.isEmpty()) {
            for (OmsOrderVo order : list) {
                if (map.get(order.getTenantId()) == null) {
                    continue;
                }
                PayExpireTimeDto dto = map.get(order.getTenantId());
                if (StrUtil.isBlank(dto.getValue())) {
                    continue;
                }
                Long timeBetweenCreateAndNow = DateUtil.between(new Date(), order.getCreateTime(), DateUnit.SECOND);
                Long expireTime = Long.parseLong(dto.getValue()) * 60;
                log.info("创建时间到现在时间的间隔：{},超时时间：{}", timeBetweenCreateAndNow, expireTime);
                if (timeBetweenCreateAndNow > expireTime) {
                    //支付超时
                    omsOrderService.setPayTimeOut(order.getId());
                } else {
                    payingList.add(order);
                }
            }
        }
        return payingList;
    }
}
